package edu.bu.met683.TravelDroid;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import edu.bu.met683.TravelDroid.data.DataUtilities;
import edu.bu.met683.TravelDroid.data.PointOfInterest;
import edu.bu.met683.TravelDroid.data.Tour;

/**
 * This class implements an activity that displays the details of a Point Of
 * Interest to the user.
 */
public class POIDetailActivity extends Activity {

	private static final String CLASS_TAG = POIDetailActivity.class
			.getSimpleName();

	private TextView poiName;
	private TextView poiDetail;
	private Button wikiButton;
	private Button closeButton;

	private PointOfInterest currentPoi;

	private MediaPlayer mp = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v(POIDetailActivity.class.getSimpleName(), " onCreate() called");

		// Retrieve the Query String
		if ((getIntent().getData() != null)
				&& (getIntent().getData().getEncodedQuery() != null)) {
			String queryString = getIntent().getData().getEncodedQuery();
			Log.v(CLASS_TAG, "onCreate(): Query String = " + queryString);

			String poiName = queryString.substring(8);
			Log.v(CLASS_TAG, "oncreate(): POI Name = " + poiName);

			// obtain the current tour from the applicaton variable that was set
			// in
			// the tour list activity
			TravelDroidApplication application = (TravelDroidApplication) this
					.getApplication();
			Tour currentTour = (Tour) application.GetCurrentTour();

			currentPoi = currentTour.getPOIByName(poiName);
		}

		if (currentPoi == null) {
			Log
					.e(CLASS_TAG,
							"onCreate(): Null POI Error - Cannot Initialize Detail Acitivy!");
			return;
		}

		this.setContentView(R.layout.poidetaillayout);

		// Set the name and text
		poiName = (TextView) findViewById(R.id.poi_name);
		poiName.setText(currentPoi.getName());
		poiDetail = (TextView) findViewById(R.id.poi_detail);
		poiDetail.setText(currentPoi.getDescription());

		// Get the Wiki button and setup button click handling
		wikiButton = (Button) findViewById(R.id.view_wiki_button);
		wikiButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (currentPoi.getWikiUrl() != null) {
					// Start Browser with Wiki Page
					Intent i = new Intent(Intent.ACTION_VIEW);
					Uri u = Uri.parse(currentPoi.getWikiUrl());
					i.setData(u);
					startActivity(i);
				}
			}
		});

		// Get a reference to the close button and handle button click
		closeButton = (Button) findViewById(R.id.close_button);
		closeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

		// Play the corresponding audio file

		String audioFileName = currentPoi.getAudioFileName();
		if (!audioFileName.equals(PointOfInterest.NO_AUDIO_FILE_STRING)) {
			Log.v(CLASS_TAG, "audioFileName = " + audioFileName);

			int audioResource = getResources().getIdentifier(
					"edu.bu.met683.TravelDroid:raw/" + audioFileName, null,
					null);

			TravelDroidApplication application = (TravelDroidApplication) this
					.getApplication();
			if (application.isAudioOn())
				mp = MediaUtilities.playMediaFromRaw(this, audioResource);
		}

		// Create a new proximityAlert
		DataUtilities.createProximityAlert(this, currentPoi);
	}

	/**
	 * 
	 */
	@Override
	protected void onPause() {
		super.onPause();
		try {
			if (null != mp)
				mp.stop();
		} catch (Exception ee) {
			Log.e(CLASS_TAG, "error stopping audio file " + ee.getMessage());
		}
	}
}
